package com.tepth.maintenancesystem.view;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.tepth.maintenancesystem.R;
import com.tepth.maintenancesystem.SysApplication;
import com.tepth.maintenancesystem.bean.User;
import com.tepth.maintenancesystem.bean.VersionUpdate;
import com.tepth.maintenancesystem.untils.FileUtils;
import com.tepth.maintenancesystem.untils.ToastUtils;
import com.tepth.maintenancesystem.untils.Untils;

import org.w3c.dom.Text;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SettingActivity extends AppCompatActivity {

    @BindView(R.id.tv_personname)
    TextView personName;

    @BindView(R.id.tv_personcode)
    TextView personCode;

    @BindView(R.id.tv_loginname)
    TextView loginName;

    @BindView(R.id.tv_orgname)
    TextView orgName;

    @BindView(R.id.tv_deptname)
    TextView deptName;

    @BindView(R.id.tv_groupname)
    TextView groupName;

    @BindView(R.id.tv_jobname)
    TextView jobName;

    @BindView(R.id.tv_my_versionname)
    TextView myVersionName;//显示版本号

    @BindView(R.id.tv_my_newversion)
    TextView myNewVersion;

    @BindView(R.id.tv_my_nfcmac)
    TextView tv_my_nfcmac;

    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        SysApplication.getInstance().addActivity(this);
        context = this;
        ButterKnife.bind(this);
        initUser();
        initVersion();

    }

    /**
     * 初始化用户
     */
    private void initUser() {
        User user = Untils.getUserList(context).get(Untils.getUserIndex(context));//取出用户
        personName.setText(user.getPersonName());
        personCode.setText(user.getPersonCode());
        loginName.setText(user.getLoginName());
        orgName.setText(user.getOrgName());
        deptName.setText(user.getDeptName());
        groupName.setText(user.getGroupName());
        jobName.setText(user.getJobName());
    }

    private void initNFCMAC() {
//         address = FileUtils.getNFC_MAC_File(FileUtils.getInnerSDCardPath() + "/tepth/NFCMAC.txt").trim();
//        if(address.equals("")) {
        String address = Untils.getNFCMAC(context);
//        }
        if (address.equals("")) {
            tv_my_nfcmac.setText("未绑定设备");
        } else {
            tv_my_nfcmac.setText(address);
        }
    }

    /**
     * 初始化版本号
     */
    private void initVersion() {
        String versionName = Untils.getAppVersionName(context);
        myVersionName.setText("当前版本:V" + versionName);
//        VersionUpdate update=Untils.getVersionUpdate(context);
//        if(!update.getAppVersion().equals(versionName)){//判断是否有新版本
//            myNewVersion.setVisibility(View.VISIBLE);
//        }else{
//            myNewVersion.setVisibility(View.GONE);
//        }
        myNewVersion.setVisibility(View.GONE);
    }

    @OnClick({R.id.tv_back, R.id.setting_out_login, R.id.setting_check_version, R.id.setting_banding_nfc})
    public void click(View view) {
        switch (view.getId()) {
            case R.id.tv_back:
                this.finish();
                break;
            case R.id.setting_out_login:
                Intent intent = new Intent();
                intent.setClass(this, LoginActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.putExtra("manual", "manual");
                startActivity(intent);
                break;
            case R.id.setting_check_version:
                ToastUtils.showToast(context, "当前为最新版本");
                break;
            case R.id.setting_banding_nfc:
                Intent intent1 = new Intent();
                intent1.setClass(this, NFCListActivity.class);
                startActivity(intent1);
                break;
        }
    }

    @Override
    protected void onResume() {
        initNFCMAC();
        super.onResume();
    }
}
